#
# Executes commands at login pre-zshrc.
#
# Authors:
#   Sorin Ionescu <sorin.ionescu@gmail.com>
#

#
# Browser
#
append_to_path() {
if [[ -d "$1" ]]; then
  path=(
    "$1"
    $path
  )
  # Ensure path array does not contain duplicates.
  typeset -gU path
else
    return 1
fi
}

if [[ -z "$BROWSER" && "$OSTYPE" == darwin* ]]; then
  export BROWSER='open'
fi

#
# Editors
#

export EDITOR="emacsclient -c"
export ALTERNATE_EDITOR=emacs
export VISUAL="emacsclient -c -nw"
export GIT_EDITOR="emacsclient -c"

#
# Language
#

if [[ -z "$LANG" ]]; then
  export LANG='en_US.UTF-8'
fi

#
# Paths
#
local_zsh_completions="${XDG_DATA_HOME:-$HOME/.local/share}/.zsh/site-functions"
if [[ -d $local_zsh_completions ]]; then
  fpath=($local_zsh_completions $fpath)
fi

# Ensure path arrays do not contain duplicates.
typeset -gU cdpath fpath mailpath path

# Set the list of directories that cd searches.
# cdpath=(
#   $cdpath
# )

# Set the list of directories that Zsh searches for programs.
if (( $+commands[luarocks] )); then
    eval `luarocks path --bin`
fi

if (( $+commands[go] )); then
    [[ -z $GOPATH ]] && [[ -d ~/code/golang ]] && export GOPATH=~/code/golang
    append_to_path $GOPATH/bin
fi

append_to_path "${HOME}/.local/bin"
append_to_path "${HOME}/bin"

path=(
  $HOME/{,s}bin(N)
  /opt/{homebrew,local}/{,s}bin(N)
  /usr/local/{,s}bin(N)
  $path
)

typeset -gU path

#
# Less
#

# Set the default Less options.
# Mouse-wheel scrolling has been disabled by -X (disable screen clearing).
# Remove -X to enable it.
if [[ -z "$LESS" ]]; then
  export LESS='-g -i -M -R -S -w -X -z-4'
fi

# lesspipe potentially harmful
unset LESSPIPE
unset LESSOPEN
unset LESSCLOSE

#
# Temporary Files
#
#
# Set TMPDIR if the variable is not set/empty or the directory doesn't exist
if [[ -z "${TMPDIR}" ]]; then
  export TMPDIR="/tmp/zsh-${UID}"
fi

if [[ ! -d "${TMPDIR}" ]]; then
  mkdir -m 700 "${TMPDIR}"
fi

TMPPREFIX="${TMPDIR%/}/zsh"

#
# Nix
#
[[ -e "${XDG_STATE_HOME-$HOME/.local/state}/nix/profile/etc/profile.d/nix.sh" ]] && . "${XDG_STATE_HOME-$HOME/.local/state}/nix/profile/etc/profile.d/nix.sh"

if [[ -d "${XDG_STATE_HOME-$HOME/.local/state}/nix/profile/share/zsh/site-functions" ]]; then
  fpath=(${XDG_STATE_HOME-$HOME/.local/state}/nix/profile/share/zsh/site-functions/ $fpath)
  typeset -gU fpath
fi

# Local Variables:
# mode: sh
# End:
